<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
    include("../../operations/connection_backup.php");
   

 	$iduser = $_REQUEST['iduser'];
 	$listid = explode(',',$_REQUEST['listid']);
 	
   backup_tables($iduser,$listid);
    function backup_tables($idusers,$listids)
    {
 		$selectusername = mysql_query("SELECT UserName from employee where IDEmployee = '".$idusers."'");
 		$valusername = mysql_fetch_assoc($selectusername);
 		
 		for ($m= 0; $m<=count($listids);$m++){
 			 set_time_limit( 0 );
 			 
 			$result = mysql_query("SELECT a.* FROM approvedoc a,employee e where a.IDEmployee=e.IDEmployee and a.IDLabel<1 
 			AND a.IDEmployee='".$idusers."' AND a.IDStatus=1 AND a.IDApproveDoc = '".$listids[$m]."'");
 			
      		 $num_fields = mysql_num_fields($result);
      		 
	 		 for ($i = 0; $i < $num_fields; $i++) {
	            while($row = mysql_fetch_row($result)) {
	            	
	                $return.= 'INSERT INTO approvedoc VALUES(';
	                for($j=0; $j<$num_fields; $j++) {
	                    $row[$j] = addslashes($row[$j]);
	                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
	                    if (isset($row[$j])) {
	                        $return .= '"' . $row[$j] . '"';
	                    } else {
	                        $return .= '""';
	                    }
	                    if ($j<($num_fields-1)) {
	                        $return.= ',';
	                    }
	                }
	                $return.= ") ;\n";
	                
	              //  print $row[2];
	            }
	        }
	        $return.="\n\n\n";
        
	        // insertap_advance
	        $resultAdvance = mysql_query("SELECT * FROM ap_advance where document_id = '".$listids[$m]."' ");
	        $num_fields_ap_advance = mysql_num_fields($resultAdvance);
	         for ($i = 0; $i < $num_fields_ap_advance; $i++) {
	            while($row = mysql_fetch_row($resultAdvance)) {
	                $return.= 'INSERT INTO ap_advance VALUES(';
	                for($j=0; $j<$num_fields_ap_advance; $j++) {
	                    $row[$j] = addslashes($row[$j]);
	                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
	                    if (isset($row[$j])) {
	                        $return .= '"' . $row[$j] . '"';
	                    } else {
	                        $return .= '""';
	                    }
	                    if ($j<($num_fields_ap_advance-1)) {
	                        $return.= ',';
	                    }
	                }
	                $return.= ") ;\n";
	                
	             //   print $row[2];
	            }
	        }
	        $return.="\n\n\n";
	        
	        // insert buying_document
	        $reultBuyingDoc = mysql_query("SELECT * FROM buying_document where document_id = '".$listids[$m]."' ");
	        $num_fields_buyingdoc = mysql_num_fields($reultBuyingDoc);
	        for ($i = 0; $i < $num_fields_buyingdoc; $i++) {
	            while($row = mysql_fetch_row($reultBuyingDoc)) {
	            	
	                $return.= 'INSERT INTO buying_document VALUES(';
	                for($j=0; $j<$num_fields_buyingdoc; $j++) {
	                    $row[$j] = addslashes($row[$j]);
	                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
	                    if (isset($row[$j])) {
	                        $return .= '"' . $row[$j] . '"';
	                    } else {
	                        $return .= '""';
	                    }
	                    if ($j<($num_fields_buyingdoc-1)) {
	                        $return.= ',';
	                    }
	                }
	                $return.= ") ;\n";
	                
	             //   print $row[2];
	            }
	        }
	        $return.="\n\n\n";
	        
	        // insert listapprove
	        $results = mysql_query("SELECT a.* FROM approvedoc a,employee e where a.IDEmployee=e.IDEmployee and a.IDLabel<1 
 			AND a.IDEmployee='".$idusers."' AND a.IDStatus=1 AND a.IDApproveDoc = '".$listids[$m]."'");
	        while ($validgroup = mysql_fetch_array($results)){
	        	
	        	$groupidapp[$m] = $validgroup[IDGroupApprove];
	        }
	        	$resultlistapp = mysql_query("SELECT * FROM listapprove where IDGroupApprove = '".$groupidapp[$m]."' ");
	        	$num_fields_lstapp = mysql_num_fields($resultlistapp);

	        	for ($i = 0; $i < $num_fields_lstapp; $i++) {
		            while($row = mysql_fetch_row($resultlistapp)) {
		                $return.= 'INSERT INTO listapprove VALUES(';
		                for($j=0; $j<$num_fields_lstapp; $j++) {
		                    $row[$j] = addslashes($row[$j]);
		                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
		                    if (isset($row[$j])) {
		                        $return .= '"' . $row[$j] . '"';
		                    } else {
		                        $return .= '""';
		                    }
		                    if ($j<($num_fields_lstapp-1)) {
		                        $return.= ',';
		                    }
		                }
		                $return.= ") ;\n";
		                
		            //    print $row[2];
		            }
		        }
		        $return.="\n\n\n";
		        
		     // insert list_reference
		     $result_refer = mysql_query("SELECT a.* FROM approvedoc a,employee e where a.IDEmployee=e.IDEmployee and a.IDLabel<1 
 				AND a.IDEmployee='".$idusers."' AND a.IDStatus=1 AND a.IDApproveDoc = '".$listids[$m]."'");
	        while ($validgroup = mysql_fetch_array($result_refer)){
	        	
	        	$groupidrefer[$m] = $validgroup[IDGroupApprove];
	        }
	        	$resultlistrefer = mysql_query("SELECT * FROM list_reference where id_group = '".$groupidrefer[$m]."' ");
	        	$num_fields_lstrefer = mysql_num_fields($resultlistrefer);

	        	for ($i = 0; $i < $num_fields_lstrefer; $i++) {
		            while($row = mysql_fetch_row($resultlistrefer)) {
		                $return.= 'INSERT INTO list_reference VALUES(';
		                for($j=0; $j<$num_fields_lstrefer; $j++) {
		                    $row[$j] = addslashes($row[$j]);
		                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
		                    if (isset($row[$j])) {
		                        $return .= '"' . $row[$j] . '"';
		                    } else {
		                        $return .= '""';
		                    }
		                    if ($j<($num_fields_lstrefer-1)) {
		                        $return.= ',';
		                    }
		                }
		                $return.= ") ;\n";
		                
		            //    print $row[2];
		            }
		        }
		        $return.="\n\n\n";
		        
		        
		        
		      	mysql_query("delete from listapprove where listapprove.IDGroupApprove='".$groupidrefer[$m]."'");
				mysql_query("delete from list_reference where list_reference.id_group='".$groupidrefer[$m]."'");
				mysql_query("delete from buying_document where buying_document.document_id='".$listids[$m]."'");
				mysql_query("delete from ap_advance where ap_advance.document_id='".$listids[$m]."'");
				mysql_query("delete from approvedoc  WHERE IDApproveDoc='".$listids[$m]."'");
		    
 		}
    	$filess = "../../backupsql/backup_$valusername[UserName]_".date("Y_m_d_H-i-s") .".sql";
		 		$realfile = "backup_$valusername[UserName]_".date("Y_m_d_H-i-s") .".sql";
		        $handle = fopen($filess,'w+');
		    	fwrite($handle,$return);
		    	fclose($handle);
    	$insertbackup = mysql_query("Insert into backup_approve(filename,username,iduser,real_filename) VALUES('".$filess."','".$valusername[UserName]."','".$idusers."','".$realfile."');");
    	mysql_close();
}
?>

<?php 
include '../../operations/connection.php';
if ($_REQUEST['newlang'] == "EN") {
			include "../../value/value_en.php";
		} else if($_REQUEST['newlang'] == "VN") {
			include "../../value/value_vn.php";
		}
		else {
			include "../../value/value_kr.php";
		}
if(!isset($_REQUEST['page'])){
$list["a.Tile"]="DESC";
$list["a.Timecreate"]="DESC";
$list["a.IDApproveDoc"]="DESC";
$list["a.TimeApprove"]="DESC";
//$str=$_SESSION['sortn'];
//if($str==''){
$str="a.TimeApprove";
$_SESSION['sortn']=$str;
//}

$_SESSION['sort']=$list;
}else{
	$str=$_SESSION['sortn'];
	$list=$_SESSION['sort'];
}
if ( !isset($_REQUEST['page'] ))
	{
    	$page = 1 ;
	}else{
		$page = $_REQUEST['page'] ;
	}
	$rows_per_page = $_SESSION['rows_page'];
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
    
    if (isset($_REQUEST['search'])){
    	$tile=$_REQUEST['titleSearch'];
    	$fromdate=$_REQUEST['date1'];
    	$todate=$_REQUEST['date2'];
    	$search=$_REQUEST['search'];
    	
    	$sql="SELECT a.SumMoney,a.IDApproveDoc,e.FullName,a.Tile,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate,a.IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as DateApprove,a.isView  
								FROM approvedoc a,employee e 
								WHERE a.IDEmployee=e.IDEmployee and a.IDLabel<1 AND a.IDEmployee='$iduser' AND a.IDStatus=1
									AND a.Tile LIKE '%$tile%' AND a.Timecreate>'".$fromdate." 00:00:00' AND a.Timecreate<'".$todate." 23:59:59'";
    	$query= mysql_query($sql." ORDER BY ".$str." ".$list[$str]);
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page ); 
    }
    else {
    	$tile="";
    	$fromdate=date("Y-m-d"); 
   		$todate=date("Y-m-d"); 
   		
   		$sql="SELECT a.SumMoney,a.IDApproveDoc,e.FullName,a.Tile,DATE_FORMAT(a.Timecreate, '%Y-%m-%d') as Timecreate,a.IDTypeDoc,DATE_FORMAT(a.TimeApprove, '%Y-%m-%d') as DateApprove,a.isView  
								FROM approvedoc a,employee e 
								WHERE a.IDEmployee=e.IDEmployee  and a.IDLabel<1 AND a.IDEmployee='$iduser' AND a.IDStatus=1 ";
    	$query= mysql_query($sql." ORDER BY ".$str." ".$list[$str]);
    	
    	$number_of_page = ceil ( mysql_num_rows( $query ) / $rows_per_page );     	
    	
    }
   
?>

<table width="100%" class="table_th" style="height:22px;" cellspacing="0">
												<tr class="trtit" style="background-color:#EEE;">
													<td width="3%"><input type="checkbox" name="chonhetroi" id="chonhetroi" onclick="clickChonHetRoi(this);"/></td>
													<td width="3%"><?=$l_no?></td>
													<!--td width="10%"><a onclick="sort(<?=$page?>,'a.IDApproveDoc','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')">ID</a></td-->
										            <td width="15%"><?=$l_document_form?></td>
										            <td width="30%"><a onclick="sort(<?=$page?>,'a.Tile','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_title?></a></td>
										 
										            <td width="10%"><a onclick="sort(<?=$page?>,'a.Timecreate','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_created_date?></a></td>
										            <td width="10%"><a onclick="sort(<?=$page?>,'a.TimeApprove','1','<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><?=$l_approve_date?></a></td>
										         
										        </tr>
												
												
												
												<?php
												
											    $i = 0; 
											    while ($row=mysql_fetch_array($query)){
											    	if ( $i >= $page_start  ) 
													{	
												    	if($row[IDTypeDoc]==7)$document = $l_vacation_form;
													    else if($row[IDTypeDoc]==12)$document = $l_assignment_report;
													    else if($row[IDTypeDoc]==13)$document = $l_purchase_order;
													    else if($row[IDTypeDoc]==14)$document = $l_daily_report;
													    else if($row[IDTypeDoc]==15)$document = $l_business_trip_report;
													    else if($row[IDTypeDoc]==16)$document = $l_weekly_report;
													    else if($row[IDTypeDoc]==17)$document = $l_susqence_payment_request;
												    
												?>
													<tr bgcolor="#ffffff" class="highlighter">
														<td><input type="checkbox" id="chon" name="xoa[]" value="<?=$row[IDApproveDoc]?>" /></td>
														<td width="3%" align="center"><?=($i+1)?></td>
														<!--td ><a href='mydocument.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>&page=<?=$page?>'><?php echo $row[IDApproveDoc];?></a></td-->
														<td ><div  align="left" ><?php echo $document;?></div></td>
														<td >
															<div  align="left" >
																<a href='mydocument&<?=$row[IDApproveDoc]?>&<?=$row[IDTypeDoc]?>&<?=$page?>'><?=$row[Tile]?></a>
																
																
																<?php 
																
																//$today=strtotime('now');
																//$d=strtotime($row['DateApprove']);
																//if ($row['isView']==0 && ($today-$d)<=30*86400){
																if ($row['isView']==0){
																	echo "<span style='font-size:10px;'><span style='color: rgb(0, 0, 255); '><em>  [".$l_new."]</em></span></span>";
																}
																?>
																<p style="float:right">
															<?php 
																if($row[IDTypeDoc]==13 || $row[IDTypeDoc]==15 || $row[IDTypeDoc]==17){
																	if($row[SumMoney]>0){
																		echo " [ ".number_format($row[SumMoney])." ]";
																	}else{
																		if($row[IDTypeDoc]==15 || $row[IDTypeDoc]==13){
																			$s1="select sum(buying_document.total) as tongtien from approvedoc,buying_document where approvedoc.IDApproveDoc=buying_document.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}else if($row[IDTypeDoc]==17){
																			$s1="select sum(ap_advance.money) as tongtien from approvedoc,ap_advance where approvedoc.IDApproveDoc=ap_advance.document_id and approvedoc.IDApproveDoc='".$row[IDApproveDoc]."' GROUP BY approvedoc.IDApproveDoc";
																			$k1=mysql_query($s1);
																			while ($r1=mysql_fetch_array($k1)){
																				if($r1[tongtien]>0){
																					echo " [ ".number_format($r1[tongtien])." ]";
																				}
																			}
																		}
																	}
																}
															?>
															</p>
															</div>
															
														</td>
														<td ><?=$row[Timecreate]?></td>
														<td ><?=$row[DateApprove]?></td>
														
													</tr>
												<?php 	}
												
													$i++; 
													
													if ($i >= $page_end) 
													{ 
													break; 
													} 
												}
												
													
												?>
											
											<tr>
													<td colspan="6" class="bt_td_main">
													
													<span style="background:#fff; padding:1px; display: inline-block;">
														
														<?php if ( $number_of_page > 1 ) 
														{ 
															if ($page==1)
																$p=2;
															else if ($page==$number_of_page)
																$p=$number_of_page-1;
															else $p=$page;
															if($page!=1){?>
																<a onclick="sort(1,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<<";?></span> </a>
																<a onclick="sort(<?=($page-1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo "<<";?></span> </a>
															<?php  }
															else{
																echo "<span class='button_next'><<<</span>"; 
																echo "<span class='button_next'><<</span>";
															}
															for ( $i = $p-1; $i <= $p+1; $i++ ) {	
																	if ($i<=$number_of_page && $i>0)
																	
																		if ($i==$page){
																			echo "<span class='button_next'><b>".$i."</b></span>"; 
																		}else{
																		?>
																			<a onclick="sort(<?=$i?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class="button_next" style="color: #508e21;">
																			<?php 
																			echo $i;
																			?>
																			</span> </a>
																		<?php 
																		}
																	?>
																
															<?php 
																	}
																
																if($page!=$number_of_page){ ?>
																	<a onclick="sort(<?=($page+1)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')"><span class='button_next'><?php echo ">>";?></span> </a>
																	<a onclick="sort(<?=($number_of_page)?>,'<?=$str?>',0,'<?=$search?>','<?=$tile?>','<?=$fromdate?>','<?=$todate?>')" ><span class='button_next'><?php echo ">>>";?></span> </a>
																<?php }
																else{
																	echo "<span class='button_next'>>></span> ";
																	echo "<span class='button_next'>>>></span> ";
																}
															}
														
															
															?>
																												
													</span>
													</td>
												</tr>
											</table>
											<?php mysql_close($connection);?>